/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.main.client;

import br.com.main.client.system.SistemaPresenter;
import br.com.main.client.system.SistemaView;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;

public class LoginPresenter {

	private Display display;
	private DeckPanel deck;

	interface Display {
		Label getCadastro();

		Button getEntrar();

		void cleanCampos();

		TextBox getLogin();

		PasswordTextBox getSenha();

		void erro();

		ListBox getLista();

		void isMorador();

		TextBox getTbIdCond();

		Label getRecuperar();
	}

	public LoginPresenter(Display tela, DeckPanel deck) {
		this.display = tela;
		this.deck = deck;
		bind();
	}

	private void bind() {
		display.getLista().addChangeHandler(new ChangeHandler() {

			@Override
			public void onChange(ChangeEvent event) {
				display.isMorador();
			}
		});

		display.getLogin().addKeyPressHandler(new PressEnter());
		display.getSenha().addKeyPressHandler(new PressEnter());

		display.getCadastro().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				deck.showWidget(1);
				display.cleanCampos();
			}
		});
		
		display.getRecuperar().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent arg0) {
				new DialogBoxRecuperarSenha();
				
			}
		});

		display.getEntrar().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				if (display.getLista().isItemSelected(0)) {
					getServico().logInSindico(display.getLogin().getText(),
							display.getSenha().getText(),
							new AsyncCallback<String>() {

								@Override
								public void onSuccess(String result) {
									if (result.equals("True")) {
										SistemaView system = new SistemaView(0);
										deck.add(system);
										new SistemaPresenter(system, deck);

										deck.showWidget(2);
										display.cleanCampos();
									} else {
										display.erro();
										System.out.println("erro");
									}
								}

								@Override
								public void onFailure(Throwable caught) {
									display.getLogin().setText("onFailure");
									deck.showWidget(2);
									display.cleanCampos();
								}

							});
				} else { // display.getLista().isItemSelected(1)
					getServico().logInMorador(display.getLogin().getText(),
							display.getSenha().getText(),
							display.getTbIdCond().getText(),
							new AsyncCallback<String>() {

								@Override
								public void onSuccess(String result) {
									if (result.equals("True")) {
										SistemaView system = new SistemaView(1);
										deck.add(system);
										new SistemaPresenter(system, deck);

										deck.showWidget(2);
										display.cleanCampos();
									} else {
										display.erro();
										System.out.println("erro");
									}
								}

								@Override
								public void onFailure(Throwable arg0) {
									// TODO Auto-generated method stub

								}
							});
				}

			}
		});

	}

	private class PressEnter implements KeyPressHandler {

		@Override
		public void onKeyPress(KeyPressEvent event) {
			if (event.getUnicodeCharCode() == KeyCodes.KEY_ENTER) {
				display.getEntrar().click();
			}
		}

	}

	private ComunServicoAsync getServico() {
		return GWT.create(ComunServico.class);
	}

}
